#!/usr/bin/env node

const Koa = require('koa'),
      Router = require('koa-router'),
      bodyparser = require('koa-bodyparser');

const app = new Koa(),
      router = new Router();

app.use(bodyparser());
router.prefix ('/api/authors');

router.get('/',async (ctx)=>{
  //page和limit要和query请求的一样
  const {page,limit} = ctx.query;
  //curl -H "abc:def" http://localhost:3000/api/authors/\?page\=22$limit\=10来请求  为请求头添加属性abc
  console.log(ctx.request.headers["abc"]);
  ctx.body = `分页查询漫画书作者,page=${page} & limit=${limit}`;
});


//测试用curl curl http://localhost:3000/api/authors/wangding/\?page\=22\&limit\=10
router.get('/:authorName',async (ctx)=>{
  const {authorName} = ctx.params;
  ctx.body = `按名称查询漫画书作者 ${authorName}`;
})

//测试用curl -X POST -d 'name=wangding&age=2' http://localhost:3000/api/authors 
router.post('/',async (ctx)=>{
  const {name,age} = ctx.request.body;
 ctx.body = `添加漫画书作者 name=${name}&age=${age}`;
});

// 测试用curl -X PUT -d 'abc' http://localhost:3000/api/authors/22
router.put('/:id',async (ctx)=>{
  const {id} = ctx.params;
 ctx.body = `修改漫画书作者 id=${id}`;
})

//测试用curl -X DELETE http://localhost:3000/api/authors/216
router.delete('/:id',async (ctx)=>{
  const {id} = ctx.params;
 ctx.body = `删除漫画书作者 id=${id}`;

})

app.use(router.routes());
app.listen(3000);
